package net.maku.im.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.im.convert.ImUserConvert;
import net.maku.im.dto.ImUserBanDTO;
import net.maku.im.dto.ImUserQueryDTO;
import net.maku.im.dto.ImUserUnbanDTO;
import net.maku.im.entity.ImUserEntity;
import net.maku.im.service.ImUserService;
import net.maku.im.vo.ImUserVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* IM用户
*
* @author Blue 
* @since 1.0.0 2024-07-14
*/
@RestController
@RequestMapping("im/user")
@Tag(name="IM用户")
@AllArgsConstructor
public class ImUserController {
    private final ImUserService userService;

    @GetMapping("findByName")
    @Operation(summary = "根据用户名或昵称查找用户",description = "用于下拉框选择用户")
    public Result<List<ImUserVO>> findByName(@RequestParam String name){
        List<ImUserVO> vos = userService.findByName(name);
        return Result.ok(vos);
    }

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('im:user:page')")
    public Result<PageResult<ImUserVO>> page(@ParameterObject @Valid ImUserQueryDTO query){
        PageResult<ImUserVO> page = userService.page(query);
        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('im:user:info')")
    public Result<ImUserVO> get(@PathVariable("id") Long id) {
        ImUserEntity entity = userService.getById(id);
        return Result.ok(ImUserConvert.INSTANCE.convert(entity));
    }

    @Operation(summary = "账号封禁")
    @PutMapping("/ban")
    @PreAuthorize("hasAuthority('im:user:ban')")
    public Result<String> ban(@RequestBody @Valid ImUserBanDTO dto){
        userService.ban(dto);
        return Result.ok();
    }


    @Operation(summary = "账号解封")
    @PutMapping("/unban")
    @PreAuthorize("hasAuthority('im:user:ban')")
    public Result<String> unban(@RequestBody @Valid ImUserUnbanDTO dto){
        userService.unban(dto);
        return Result.ok();
    }
}